package com.example.advancecomponents;

import static com.example.advancecomponents.utils.StarUtil.plantList;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.advancecomponents.adaptors.CustomizedAdaptor;
import com.example.advancecomponents.vo.Star;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        if (getSharedPreferences("config",MODE_PRIVATE).getBoolean("firstBoot",true)) {
            // 跳转欢迎页面
            Intent intent = new Intent(this, IndexPageActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return;
        }

        Spinner selectGame = findViewById(R.id.selectGame);

        ArrayList<String> data = new ArrayList<>();

        data.add("黑神话");
        data.add("电脑");
        data.add("笔记本");

        selectGame.setAdapter(new ArrayAdapter<>(this, R.layout.simple_spinner_item, data));

        selectGame.setOnItemSelectedListener(this);

        Spinner selectImg = findViewById(R.id.selectImg);

        ArrayList<Map<String, Object>> simpleSpinnerData = getSimpleSpinnerData();


        selectImg.setAdapter(new SimpleAdapter(this, simpleSpinnerData, R.layout.composed_view,
                new String[]{
                        "icon",
                        "detail"
                },
                new int[]{
                        R.id.icon,
                        R.id.itemDetail
                }));

        Spinner customizedAdaptorSpinner = findViewById(R.id.customizedAdaptor);

        customizedAdaptorSpinner.setAdapter(new CustomizedAdaptor(this, plantList()));


        
        ListView plantList = findViewById(R.id.plantList);

        plantList.setAdapter(new CustomizedAdaptor(this,plantList()));


        plantList.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id)->{

            Toast.makeText(this, "ListView中的条目被点击了"+position, Toast.LENGTH_SHORT).show();
        });
        CheckBox showDivider = findViewById(R.id.showDivider);

        showDivider.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked)->{
            if (isChecked) {
                Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.color.black, getTheme());
                plantList.setDivider(drawable);
                plantList.setDividerHeight(10);
            }else{
                plantList.setDivider(null);
            }
        });

        CheckBox changeClickedBackGround = findViewById(R.id.changeClickedBackGround);

        changeClickedBackGround.setOnCheckedChangeListener((view,isChecked)->{
            if (isChecked) {
                plantList.setSelector(R.drawable.list_view_selector);
            }else{
                plantList.setSelector(R.color.transparent);
            }
        });

        // 跳转详情页
        findViewById(R.id.goToDetailPage).setOnClickListener(view->{
            Intent intent = new Intent(this, PagerTabStripActivity.class);
            startActivity(intent);
        });

        // 跳转详情页(使用 Fragment)
        findViewById(R.id.goToDetailPageByFragment).setOnClickListener(view->{
            Intent intent = new Intent(this, PagerTabStripActivity.class);
            intent.putExtra("useFragment",true);
            startActivity(intent);
        });


    }


    private static @NonNull ArrayList<Map<String, Object>> getSimpleSpinnerData() {
        ArrayList<Map<String, Object>> simpleSpinnerData = new ArrayList<>();

        HashMap<String, Object> row1 = new HashMap<>();
        row1.put("icon", R.drawable.earth);
        row1.put("detail", "地球");
        simpleSpinnerData.add(row1);

        HashMap<String, Object> row2 = new HashMap<>();
        row2.put("icon", R.drawable.jupiter);
        row2.put("detail", "木星");
        simpleSpinnerData.add(row2);
        return simpleSpinnerData;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "选中了:" + ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}